package com.easemob.chat;

import android.content.Context;
import android.content.Intent;
import com.easemob.EMNotifierEvent.Event;
import com.easemob.chat.core.a;
import com.easemob.chat.core.e;
import com.easemob.util.DateUtils;
import com.easemob.util.EMLog;
import com.easemob.util.PathUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.StringUtils;

class ChatListener
  implements PacketListener
{
  private static final String TAG = "chat";
  protected String previousFrom = "";
  protected String previousBody = "";
  protected long previousTime = System.currentTimeMillis();
  protected EMChatManager chatManager = null;
  protected ExecutorService recvThreadPool = null;
  protected ArrayBlockingQueue<String> recentMsgIdQueue;
  protected static final int RECENT_QUEUE_SIZE = 20;
  private EMOfflineMessageHandler offlineHandler = null;

  public ChatListener(EMChatManager paramEMChatManager)
  {
    this.chatManager = paramEMChatManager;
    this.recvThreadPool = Executors.newCachedThreadPool();
    this.recentMsgIdQueue = new ArrayBlockingQueue(20);
    this.offlineHandler = new EMOfflineMessageHandler();
  }

  protected static void ackMessage(Message paramMessage)
  {
    String str = paramMessage.getPacketID();
    if ((str == null) || (str.equals("")))
      return;
    PacketExtension localPacketExtension = paramMessage.getExtension("urn:xmpp:receipts");
    if (localPacketExtension != null)
      return;
    Message localMessage1 = new Message();
    localMessage1.setPacketID(str);
    localMessage1.setTo(EMChatConfig.DOMAIN);
    localMessage1.setFrom(paramMessage.getTo());
    a locala1 = new a("received");
    locala1.setValue("id", str);
    localMessage1.addExtension(locala1);
    EMSessionManager.getInstance().getConnection().sendPacket(localMessage1);
    EMLog.d("chat", "send ack message back to server:" + localMessage1);
    if ((paramMessage.getType() == Message.Type.chat) && (EMChatManager.getInstance().getChatOptions().getRequireDeliveryAck()))
    {
      Message localMessage2 = new Message();
      localMessage2.setTo(paramMessage.getFrom());
      localMessage2.setFrom(paramMessage.getTo());
      a locala2 = new a("delivery");
      locala2.setValue("id", str);
      localMessage2.addExtension(locala2);
      localMessage2.setBody(str);
      EMLog.d("chat", "send delivered ack msg to:" + paramMessage.getFrom() + " for msg:" + str);
      localMessage2.setType(Message.Type.normal);
      EMSessionManager.getInstance().getConnection().sendPacket(localMessage2);
      e.a().h(str, true);
    }
  }

  protected boolean isDuplicateMsg(Message paramMessage)
  {
    boolean bool = false;
    if ((paramMessage.getFrom().equals(this.previousFrom)) && (paramMessage.getBody().equals(this.previousBody)) && (System.currentTimeMillis() - this.previousTime < 1000L))
    {
      EMLog.d("chat", "ignore duplicate msg with same from and body:" + this.previousFrom);
      bool = true;
    }
    this.previousFrom = paramMessage.getFrom();
    this.previousBody = paramMessage.getBody();
    this.previousTime = System.currentTimeMillis();
    String str1 = paramMessage.getPacketID();
    if (str1 == null)
      return bool;
    Iterator localIterator = this.recentMsgIdQueue.iterator();
    while (localIterator.hasNext())
    {
      String str2 = (String)localIterator.next();
      if (str1.equals(str2))
      {
        EMLog.d("chat", "ignore duplicate msg:" + paramMessage);
        return true;
      }
    }
    if (this.recentMsgIdQueue.size() == 20)
      try
      {
        this.recentMsgIdQueue.poll();
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
    this.recentMsgIdQueue.add(paramMessage.getPacketID());
    bool = false;
    return bool;
  }

  protected boolean processMessage(Message paramMessage)
  {
    ackMessage(paramMessage);
    if ((paramMessage.getBody() == null) || (paramMessage.getBody().equals("")))
      return true;
    if (isDuplicateMsg(paramMessage))
    {
      EMLog.d("chat", "ignore duplicate msg");
      return true;
    }
    String str = StringUtils.parseBareAddress(paramMessage.getFrom());
    EMLog.d("chat", "chat listener receive msg from:" + str + " body:" + paramMessage.getBody());
    if (paramMessage.getType() != Message.Type.chat)
      return false;
    EMMessage localEMMessage = MessageEncoder.parseXmppMsg(paramMessage);
    if (paramMessage.getExtension("encrypt", "jabber:client") != null)
      localEMMessage.setAttribute("isencrypted", true);
    return processEMMessage(localEMMessage);
  }

  protected boolean processEMMessage(EMMessage paramEMMessage)
  {
    if (paramEMMessage == null)
      return false;
    if (paramEMMessage.getMsgId() == null)
      paramEMMessage.msgId = DateUtils.getTimestampStr();
    if (paramEMMessage.type == EMMessage.Type.CMD)
    {
      if (EMChat.getInstance().appInited)
        sendCmdMsgBroadcast(paramEMMessage);
      else
        EMChatManager.getInstance().offlineCmdMessagesList.add(paramEMMessage);
      return true;
    }
    if (((paramEMMessage.body instanceof FileMessageBody)) && (paramEMMessage.getType() != EMMessage.Type.FILE))
    {
      setLocalUrl(paramEMMessage);
      boolean bool = paramEMMessage.getBooleanAttribute("isencrypted", false);
      ReceiveMessageThread localReceiveMessageThread = new ReceiveMessageThread(paramEMMessage, bool);
      this.recvThreadPool.execute(localReceiveMessageThread);
    }
    else
    {
      if (paramEMMessage.getType() == EMMessage.Type.FILE)
        setLocalUrl(paramEMMessage);
      paramEMMessage.status = EMMessage.Status.SUCCESS;
    }
    EMChatManager.getInstance().saveMessage(paramEMMessage);
    if (paramEMMessage.offline)
    {
      this.offlineHandler.onNewOfflineMessage(paramEMMessage);
    }
    else
    {
      this.offlineHandler.stop();
      this.chatManager.notifyMessage(paramEMMessage);
    }
    return true;
  }

  void sendCmdMsgBroadcast(EMMessage paramEMMessage)
  {
    if (EMNotifier.getInstance(EMChat.getInstance().getAppContext()).publishEvent(EMNotifierEvent.Event.EventNewCMDMessage, paramEMMessage))
      return;
    Context localContext = EMChatConfig.getInstance().getApplicationContext();
    Intent localIntent = new Intent(EMChatManager.getInstance().getCmdMessageBroadcastAction());
    localIntent.putExtra("msgid", paramEMMessage.getMsgId());
    localIntent.putExtra("message", paramEMMessage);
    EMLog.d("chat", "received cmd message: " + paramEMMessage.getMsgId());
    localContext.sendOrderedBroadcast(localIntent, null);
  }

  void sendOfflineMsgBroadcast()
  {
    this.offlineHandler.onAppReady();
  }

  protected void setLocalUrl(EMMessage paramEMMessage)
  {
    FileMessageBody localFileMessageBody = (FileMessageBody)paramEMMessage.body;
    String str = localFileMessageBody.remoteUrl.substring(localFileMessageBody.remoteUrl.lastIndexOf("/") + 1);
    if (paramEMMessage.type == EMMessage.Type.IMAGE)
      localFileMessageBody.localUrl = (PathUtil.getInstance().getImagePath() + "/" + str);
    else if (paramEMMessage.type == EMMessage.Type.VOICE)
    {
      if (!EMChatManager.getInstance().getChatOptions().getAudioFileWithExt())
        localFileMessageBody.localUrl = (PathUtil.getInstance().getVoicePath() + "/" + str);
      else
        localFileMessageBody.localUrl = (PathUtil.getInstance().getVoicePath() + "/" + str + ".amr");
    }
    else if (paramEMMessage.type == EMMessage.Type.VIDEO)
      localFileMessageBody.localUrl = (PathUtil.getInstance().getVideoPath() + "/" + str);
    else if (paramEMMessage.type == EMMessage.Type.FILE)
      localFileMessageBody.localUrl = (PathUtil.getInstance().getFilePath() + "/" + localFileMessageBody.fileName);
    else
      localFileMessageBody.localUrl = (PathUtil.getInstance().getVideoPath() + "/" + str);
  }

  public synchronized void processPacket(Packet paramPacket)
  {
    if (!(paramPacket instanceof Message))
    {
      EMLog.d("chat", "packet is not message, skip");
      return;
    }
    Message localMessage = (Message)paramPacket;
    processMessage(localMessage);
  }

  void clearRecentMsgIdQueue()
  {
    this.recentMsgIdQueue.clear();
  }

  void resetOfflineFlags()
  {
    this.offlineHandler.reset();
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.ChatListener
 * JD-Core Version:    0.6.1
 */